! Huistaak 2 TWS
!
! Roel Matthysen
! s0202264
! 1e Master Wiskundige Ingenieurstechnieken
!
!
!
!
!
!

module my_module
    implicit none
    private
    save

    public play_with_array, show_array, integer_hist

    interface show_array
        module procedure show_vector_integer
        module procedure show_vector_real
        module procedure show_matrix_integer
        module procedure show_matrix_real
    end interface


contains
    
    
    subroutine play_with_array(x,v,w)
    real, dimension(:,:) :: x
    integer, dimension(:), intent(out) :: v
    logical, intent(out) :: w
    where(x.lt.0.6.and.x.ge.0.4) x = 0.5
    v = maxval(int(floor(x*10)),1)
    where(v >= 8) 
        v = 42
    elsewhere 
        v = 0
    endwhere
    w = all(v==42)
    end subroutine play_with_array

    

    subroutine integer_hist(v,sel)
    integer, dimension(:) :: v
    integer, dimension(:), optional :: sel
    integer, dimension(:), allocatable :: c
    integer i
    allocate(c(minval(v):maxval(v)))
    c = (/ (count(v.eq.i),i=minval(v),maxval(v)) /) 
    if(present(sel)) then
        where(sel < minval(v) .or. sel > maxval(v)) 
            sel = 0
        elsewhere
            sel = c(sel)
        endwhere
    else
        do i=minval(v),maxval(v)
            if(c(i) /= 0) then
                print *, c(i), 'keer', i
            endif
        enddo
    endif
    end subroutine


    ! Print a vector of integers in one column
    subroutine show_vector_integer(x)
    integer, dimension(:) :: x
    integer :: i
    do i = 1,size(x)
    print '(i3)',x(i)
    enddo
    end subroutine show_vector_integer
    
    ! Print a vector of reals in one column
    subroutine show_vector_real(x)
    real, dimension(:) :: x
    integer :: i
    do i = 1,size(x)
    print '((f4.2))', x(i)
    enddo
    end subroutine show_vector_real



    ! This subroutine prints a matrix of integers, 10 columns at a time, with row numbers.
    subroutine show_matrix_integer(x)
    integer, dimension(:,:) :: x
    integer :: i,j,k
    ! Execute 10 columns at a time
    do j = 1,int(floor(size(x,2)/10.0))
    ! Print column numbers
        print '(a10,3x,10(i4,3x))' , ' ', (/ (10*(j-1)+i,i=1,10) /)
        ! Print the rows
        do i = 1 , size(x,1)
            print '(i8,5x,10(i4,3x))',i, x(i,10*j-9:10*j)
        enddo
        print *, ' '
    enddo
    ! Execute the remaining columns
    k = mod(size(x,2),10)
    if(k > 0) then
        print '(a10,3x,10(i4,3x))' , ' ', (/ (10*(j-1)+i,i=1,k) /)
        do i = 1 , size(x,1)
            print '(i8,5x,10(i4,3x))',i, x(i,10*j-9:)
        enddo
    endif
    end subroutine show_matrix_integer


    ! This subroutine prints a matrix of reals, 10 columns at a time, with row numbers.
    subroutine show_matrix_real(x)
    real, dimension(:,:) :: x
    integer :: i,j,k
    ! Execute 10 columns at a time
    do j = 1,int(floor(size(x,2)/10.0))
        ! Print column numbers
        print '(a10,3x,10(i4,3x))' , ' ', (/ (10*(j-1)+i,i=1,10) /)
        ! Print the rows
        do i = 1 , size(x,1)
            print '(i8,5x,10(f4.2,3x))',i, x(i,10*j-9:10*j)
        enddo
        print *, ' '
    enddo
    ! Execute remaining columns
    k = mod(size(x,2),10)
    if(k > 0) then
        print '(a10,3x,10(i4,3x))' , ' ', (/ (10*(j-1)+i,i=1,k) /)
        do i = 1 , size(x,1)
            print '(i8,5x,10(f4.2,3x))',i, x(i,10*j-9:)
        enddo
    endif
    end subroutine
    

end module